/**
 * Created by lesonli on 2016/11/17.
 */
import * as types from '../types'
import api from '../../api/modules/config'
import {
    getList,
    addDealer,
    updateDealer,
    switchState,
    getSelectionList
} from '../../api/modules/dealer'

const state = {
    agency_list: [],
    agency_selection_list:[],
    showLoading: false,
    total: 100,
    type_list: [{ id: 1, name: '董事合伙人' }, { id: 2, name: '区域合伙人' }, { id: 3, name: '城市合伙人' }],
    state_list: [{ id: 0, name: '待审核' }, { id: 1, name: '审核通过' }, { id: 2, name: '审核未通过' }, { id: 3, name: '冻结' }]
}

// getters
// actions
const actions = {
    get_agency_list({
            commit
        }, param) {
        commit(types.AGENCY_SHOW_LOADING, param);
        getList(param).then((res) => {
            if (res.data.res_code === 1) {
                commit(types.AGENCY_LIST_LOADED, res.data.msg);
            }
        });
    },
    get_agency_selection_list({
            commit
        }) {
            if(state.agency_selection_list.length === 0)
            {
                 commit(types.AGENCY_SHOW_LOADING);
        getSelectionList().then((res) => {
            if (res.data.res_code === 1) {
                commit(types.AGENCY_SELECTION_LIST_LOADED, res.data.msg);
            }
        });
            }
       
    },
    add_agency({
            commit
        }, param) {
        commit(types.AGENCY_SHOW_LOADING, param);
        addDealer(param).then((res) => {
            if (res.data.res_code === 1) {
                commit(types.AGENCY_ADDED, { result: res.data.msg, data: param });
                if (param._fn)
                    param._fn();
            }
        });
    },
    update_agency({
            commit
        }, param) {
        commit(types.AGENCY_SHOW_LOADING, param);
        updateDealer(param).then((res) => {
            if (res.data.res_code === 1) {
                commit(types.AGENCY_UPDATED, param);
                if (param._fn)
                    param._fn();
            }
        });
    },
    delete_agency({
            commit
        }, param) {
        commit(types.AGENCY_SHOW_LOADING, param);
        switchState(param, -1).then((res) => {
            if (res.data.res_code === 1) {
                commit(types.AGENCY_DELETED, param);
                if (param._fn)
                    param._fn();
            }
        });
    },
    update_agency_state({
            commit
        }, param) {
        commit(types.AGENCY_SHOW_LOADING, param);
        switchState(param.id, param.value).then((res) => {
            if (res.data.res_code === 1) {
                commit(types.AGENCY_UPDATED, param);
                if (param._fn)
                    param._fn();
            }
        });
    },
    clear_agency({commit}) {
        commit(types.AGENCY_CLEAR);
    }
}
// mutations
const mutations = {
    [types.AGENCY_SHOW_LOADING](state, param) {
        state.showLoading = true;
    },
    [types.AGENCY_CLEAR](state) {
        state.agency_list = [];
    },
    [types.AGENCY_LIST_LOADED](state, param) {
        // state.agency_list = param.list;
        for(var i=0;i<param.length;i++)
        {
            param[i].id = param[i].dealer_id;
        }
        state.agency_list = param;
        state.total = param.total;
        state.showLoading = false;
    },
    [types.AGENCY_SELECTION_LIST_LOADED](state, param) {
        // state.agency_list = param.list;
        state.agency_selection_list = param;
        state.showLoading = false;
    },
    [types.AGENCY_ADDED](state, param) {
        state.agency_list.push({
            id: param.result,
            dealer_id:param.result,
            code: param.data.code,
            company: param.data.company,
            contact: param.data.contact,
            phone1: param.data.phone1,
            phone2: param.data.phone2,
            position_id: param.data.position_id,
            position_name: param.data.position_name,
            province_id: param.data.province_id,
            city_id: param.data.city_id,
            type: param.data.type,
            sale_user_id: param.data.sale_user_id,
            sale_user_realname: param.data.sale_user_realname,
            state: 0
        })
        state.showLoading = false;
    },
    [types.AGENCY_UPDATED](state, param) {
        for (var i = 0; i < state.agency_list.length; i++) {
            if (state.agency_list[i].id === param.id) {
                if (!param.hasOwnProperty('value')) {
                    state.agency_list[i].code = param.code;
                    for(var p in param)
                    {
                        state.agency_list[i][p] = param[p]
                    }
                }
                else
                {
                    state.agency_list[i].state = param.value;
                }
                
            }
        }
        state.showLoading = false;
    },
    [types.AGENCY_DELETED](state, param) {
        for (var i = 0; i < state.agency_list.length; i++) {
            if (state.agency_list[i].id === param.id) {
                state.agency_list.splice(i, 1);
                break;
            }
        }
        state.showLoading = false;
    }
}

export default {
    state,
    actions,
    mutations
}
